var express = require("express");
var router = express.Router();
const db = require("../db/index.js");

// 用这个包来生成 Token 字符串
const jwt = require("jsonwebtoken");
// 导入配置文件
const config = require("../config");
// 引入支付宝沙箱配置
const alipaySdk = require('../db/alipay')
const AlipayFormData = require('alipay-sdk/lib/form').default

// 查询报名列表
router.get("/applicationList", function (req, res, next) {
    console.log(req.query);
    // 获取参数   当前页码   当前每页数据量
    // 获取参数   当前页码   当前每页数据量
    let pageNumber = req.query.pageNumber;
    // let pageSize = parseInt(req.body.pageSize)
    let pagesize = req.query.pagesize - 0;
    // 求得sql语句中的参数
    console.log(pageNumber, pagesize);
    //  if(!pageNumber) pageNumber=1
    //  if(!pagesize) pagesize=6
    console.log(pageNumber, pagesize);
    let start = (pageNumber - 1) * pagesize;
    // sql语句
    let sql = "select * from application limit ?, ?";
    let sqlParam = [start, pagesize];
    let sqlCount = "select count(*) from application";
  
    var count = 0;
    // 执行查询
    db.query(sqlCount, null, (err, result) => {
      // 得到总记录数
      count = result[0]["count(*)"];
    });
  
    db.query(sql, sqlParam, (err, result) => {
      if (err) {
        res.send({
          code: 0,
          message: err.message,
          ok: true,
        });
      } else {
        res.send({
          code: 200,
          message: "请求成功",
          ok: true,
          totalCount: count,
          data: result,
        });
      }
    });
  });

//   删除报名信息
router.post("/deleteApp", function (req, res) {
    console.log(req.query);
   
    let id = req.query.id;
    const sql = "delete from application where id=?";
    db.query(sql, id, function (err, result) {
      if (err) res.send({ code: 0, message: err.message });
      res.send({ code: 200, message: "取消报名成功！" });
    });
  });

//   审核报名信息
router.post('/check',function(req,res){
    console.log(req.body);
    let info =req.body
    const sql = `update application set status=? where id=?`
    db.query(sql,[info.status,info.id],function(err,result){
        if(err) res.send({code:0,message:err.message})
        res.send({code:200,message:'审核完成！'})
    })
})

// 缴费
router.post('/money',function(req,res,next){
    let id = req.query.id
    //对接支付宝API
  const formData = new AlipayFormData();
  //调用setMethod 并传入get,会返回可以跳转到支付页面的url,
  formData.setMethod("get");
  //支付时信息
  const bizContent = {
    
    outTradeNo: id,
    productCode: 'FAST_INSTANT_TRADE_PAY',
    totalAmount: '0.01',
    subject: '商品',
    body: '商品详情',
    
  };
  formData.addField("bizContent", bizContent);
  
  
  const result = alipaySdk.exec(
    "alipay.trade.page.pay",
    {},
    { formData: formData }
  );
  const sql =`update application set money=? where id=?`
  db.query(sql,['已支付',id])
    //对接成功，支付宝返回的数据
  result.then((resp) => {
    res.send(
       {
        code: 200,
        success: true,
        msg: "支付中",
        paymentUrl: resp,
       },
    );
  });



})
// 查询本人报名信息
router.get("/myApplication", function (req, res, next) {
 
  const sql = `select * from application where nickname=?`
  db.query(sql,req.query.nickname,function(err,results){
    console.log(results);
    if(err) { return res.send({code:0,message:err.message})}
    res.send({code:200,message:'查询成功！',data:results})
  })
  
});


module.exports = router